package com.itheima04.aop;

import com.itheima.case2.dao.UserDao;
import com.itheima.case2.pojo.po.User;
import com.itheima.case2.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
//被代理类
public class UserSerivceImpl3 implements UserService3{

   /* public void deleteUser(Integer id) {
        //固定: 不允许事务自动提交
        SqlSession sqlSession = SqlSessionUtil.getSession(false);
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        System.out.println("ud:" + userDao);
        try {
            //不固定: 先删除此用户对应的所有角色,再删除角色本身
            userDao.deleteRoleById(id);
            Integer count = userDao.deleteUserById(id);

            if(count==0) {
                throw new RuntimeException("根据id删除用户失败!");
            }
            //固定: 提交事务
            SqlSessionUtil.commitAndClose(sqlSession);
        } catch (RuntimeException e) {
            //固定: 回滚事务
            SqlSessionUtil.rollbackAndClose(sqlSession);
            throw new RuntimeException(e.getMessage());
        }
    }*/

   /*
   *    在spring的aop中
   *        我们无需关注事务的编写,只要关注业务部分
   *        事务只要声明即可
   * */
    public void deleteUser(Integer id) {
        UserDao userDao = ProxyUtil.userDao;

        //不固定: 先删除此用户对应的所有角色,再删除角色本身
        userDao.deleteRoleById(id);
        Integer count = userDao.deleteUserById(id);

        if(count==0) {
            throw new RuntimeException("根据id删除用户失败!");
        }

    }


}
